/**
 * @author DongPing
 * 2022/1/17 17:55
 *
 * 门店相关数据查询
 *
 * */
import {generateCacheRequestFunction} from "@/common-data-source/WithCache";
import {TSelectOptions} from "@/common-data-source/type";
import {AsyncPageSelectSearchParam} from "@/biz-components/claim-items/components";
import {xhrPostJson} from "@/services";
import {parsePageResponseToSelect} from "@/common-data-source/parse-page-response-to-select";


/**
 * 查询店铺的供应商 分页 @TODO shop
 * */
export function queryShopSuppliersPage(params: AsyncPageSelectSearchParam, shop: string): Promise<{data: TSelectOptions, total: number}> {
	return xhrPostJson('/asm/asmSupplier/page',
		{"queryKeys":{"keyword":params.keyword, companyCode: shop, group: ['Z010', 'Z030']},"pageNo":params.pageNo,"pageSize":params.pageSize,"pageCode":"Supplier_APP_L"}
	)
		.then(parsePageResponseToSelect)
}

/**
 * 查询店铺下的客户
 * */
export const queryShopCustomers = generateCacheRequestFunction<TSelectOptions, string>(function (params) {
	return xhrPostJson('/asm/custExt/page', {"queryKeys":{"keyword":""},"pageNo":1,"pageSize":100000000})
		.then((res: any) => {
			const data: TSelectOptions = [];
			for(const item of res.result.content) {
				data.push({name: item.name, value: item.code})
			}
			return data;
		})
});
/**
 * 分页查询店铺下的客户
 * */
export function queryShopCustomersPage(param: AsyncPageSelectSearchParam, shop: string): Promise<{data: TSelectOptions, total: number}> {
	return xhrPostJson('/asm/custExt/page', {"queryKeys":{"keyword":param.keyword, company: shop},"pageNo":param.pageNo,"pageSize":param.pageSize})
		.then(parsePageResponseToSelect)
}

/**
 * 认领门店下的员工供应商查询
 * 1、认领门店下的员工供应商列表（查询供应商主数据，供应商类型为Z040)

2、默认值：通过付款方银行账号匹配对应的客户
 * */
export function queryShopEmployeeSuppliersPage(param: AsyncPageSelectSearchParam, shop: string):  Promise<{data: TSelectOptions, total: number}> {
	return xhrPostJson('/asm/asmSupplier/page', {"queryKeys":{"keyword":param.keyword, group: 'Z040', companyCode: shop},"pageNo":param.pageNo,"pageSize":param.pageSize})
		.then(parsePageResponseToSelect)
	// return Promise.resolve({
	// 	data: [],
	// 	total: 0
	// })
}

/**
 * 认领门店下的柜组供应商列表:（查询供应商主数据，供应商类型为Z050)
 * */
export function queryShopSaleBarSuppliersPage(param: AsyncPageSelectSearchParam, shop: string):  Promise<{data: TSelectOptions, total: number}> {
	return xhrPostJson('/asm/asmSupplier/page', {"queryKeys":{"keyword":param.keyword, group: 'Z050', companyCode: shop},"pageNo":param.pageNo,"pageSize":param.pageSize})
		.then(parsePageResponseToSelect)
	// return Promise.resolve({
	// 	data: [],
	// 	total: 0
	// })
}

/**
 * 查询门店下面所有人员，
 * 柜组经办人
 * */
export const queryShopPersons = generateCacheRequestFunction<TSelectOptions, string>(function (shop) {
	return Promise.resolve([{name: 'person-a', value: 'pa'}]);
});
export function queryShopPersonsPage(params: AsyncPageSelectSearchParam, shop: string): Promise<{total: number, data: TSelectOptions}> {
	return queryShopEmployees(params, shop);
}

/**
 * 查询门店下面所有员工
 * */
export const queryShopEmployees = function (params: AsyncPageSelectSearchParam, shop: string): Promise<{total: number, data: any[]}> {
	if(!shop) return Promise.resolve({total: 0, data: []});
	return xhrPostJson('/sym/orgMaint/page', {
		queryKeys: {
			companyCode: shop,

			value: params.keyword
		},
		pageNo: params.pageNo,
		pageSize: params.pageSize }
	)
		.then(parsePageResponseToSelect)

}

/**
 * 查询门店下所有柜组
 * */
export const queryShopSaleBars = generateCacheRequestFunction<TSelectOptions, string>(function (shop) {
	return Promise.resolve([{name: 'SaleBar-1', value: 's1'}]);
});
export function queryShopSaleBarsPage(p: AsyncPageSelectSearchParam, shop: string): Promise<{total: number, data: TSelectOptions}> {
	if(!shop) return Promise.resolve({total: 0, data: []})
	return xhrPostJson('sym/sapDept/page', {
		queryKeys: {
			value: p.keyword,
			companyCode: shop
		},
		pageNo: p.pageNo,
		pageSize: p.pageSize
	})
		.then((res: any) => parsePageResponseToSelect(res, 'code', 'rentName'))
}
/**
 * 查询sap公司下的部门
 * */
export function querySAPShopDepartmentsPage(p: AsyncPageSelectSearchParam, shop: string): Promise<{total: number, data: TSelectOptions}> {
	if(!shop) {
		return Promise.resolve({total: 0, data: []});
	}
	return xhrPostJson('/sym/department/page', {
		 queryKeys: {
		 	companyCode: shop
		 },
		pageNo: p.pageNo,
		pageSize: p.pageSize
		})
		.then(parsePageResponseToSelect)
}

/**
 * 查询所有门店
 * */

export function queryAllShopPage(p: AsyncPageSelectSearchParam): Promise<{total: number, data: TSelectOptions}> {
	return xhrPostJson('sym/sapCompany/page', {
		queryKeys: {
			value: p.keyword
		},
		pageNo: p.pageNo,
		pageSize: p.pageSize
	})
		.then(parsePageResponseToSelect)
}

/**
 * 在建工程资产
 * */
export const queryProjectAssetsPage = generateCacheRequestFunction<{data: any[], total: number}, AsyncPageSelectSearchParam>(function (params) {
	return xhrPostJson('/asm/assets/page', {
		"queryKeys":{
			"BUKRS":"1010",
			"pageCode":"Assets_PC_L"
		},
		"pageNo": params.pageNo,
		"pageSize": params.pageSize
	})
		.then((res: any) => {
			return parsePageResponseToSelect(res, 'code', 'TXT50')
		})
});


